<?php
/**
 * Description: 获取面试题目详情
 * User: devforma
 * Date: 2016/11/14
 * Time: 11:21
 */

class InterviewPaperDetail implements iController {
    private $questionStatus = [
        '1' => 'analysis',
        '2' => 'notice'
    ];

    /**
     * api请求处理主函数
     * @param array $queryArgs 就是$_REQUEST变量
     * @return array api返回结果，必须含response_code
     */
    public function output($queryArgs) {
        if (empty($queryArgs['paper_id']) || filter_var($queryArgs['paper_id'], FILTER_VALIDATE_INT) === false) {
            return genOutput(1000);
        }

        //获取题目列表
        $questions = $this->getInterviewQuestions($queryArgs['paper_id']);
        if (empty($questions))
            return genOutput(1001);

        //获取题目材料、知识点、来源
        $materialIds = [];
        $qids = [];
        foreach ($questions as $q) {
            $qids[] = $q->id;

            if (!empty($q->material_id) && !in_array($q->material_id, $materialIds, true)) {
                $materialIds[] = $q->material_id;
            }
        }

        $materials = $this->getInterviewMaterial($materialIds);
        $notes     = $this->getInterviewNotes($qids);
        $papers    = $this->getQuestionPapers($qids);

        //构造返回结果
        $list = [];
        foreach ($questions as $q) {
            $list[] = [
                'id'       => (int)$q->id,
                'material' => isset($materials[$q->material_id]) ? html2text($materials[$q->material_id]) : '',
                'question' => html2text($q->question_content),
                'notes'    => isset($notes[$q->id]) ? implode(' ', $notes[$q->id]) : '',
                'analysis' => html2text($q->reference_text),
                'keywords' => str_replace('#', ',', $q->keywords),
                'from'     => isset($papers[$q->id]) ? implode(' ', $papers[$q->id]) : '',
                'status'   => $this->questionStatus[$q->status]
            ];
        }

        return genOutput(1, ['questions' => $list]);
    }

    /**
     * 获取试题的试卷信息
     *
     * @param array $qids 试题id
     * @return array
     */
    private function getQuestionPapers($qids) {
        global $cmsdb;

        $list = [];

        $string = implode(',', $qids);
        $sql = "SELECT p.name, r.question_id FROM `interview_papers` AS p INNER JOIN `interview_relation_paper_question` AS r ON p.id=r.paper_id AND r.question_id IN ({$string})";
        $papers = (array)$cmsdb->getResults($sql);

        foreach ($papers as $paper) {
            if (isset($list[$paper->question_id])) {
                $list[$paper->question_id][] = $paper->name;
            } else {
                $list[$paper->question_id] = [$paper->name];
            }
        }

        return $list;
    }


    /**
     * 获取面试知识点
     *
     * @param array $qids 题目id
     * @return array
     */
    private function getInterviewNotes($qids) {
        global $cmsdb;

        $list = [];

        $string = implode(',', $qids);
        $sql = "SELECT n.term_name, r.question_id FROM `interview_relation_note_question` AS r INNER JOIN `interview_note_term` AS n ON r.note_id=n.id WHERE r.question_id IN ({$string})";
        $notes = (array)$cmsdb->getResults($sql);

        foreach ($notes as $n) {
            if (isset($list[$n->question_id])) {
                $list[$n->question_id][] = $n->term_name;
            } else {
                $list[$n->question_id] = [$n->term_name];
            }
        }

        return $list;
    }


    /**
     * 获取面试题目
     *
     * @param int $paperId 试卷id
     * @return array
     */
    private function getInterviewQuestions($paperId) {
        global $cmsdb;

        $sql = 'SELECT q.* FROM `interview_questions` AS q INNER JOIN `interview_relation_paper_question` AS r ON q.id=r.question_id WHERE r.paper_id=:pid AND q.status!=0 ORDER BY r.number ASC, r.id ASC';
        $questions = (array)$cmsdb->getResults($sql, [':pid' => $paperId]);

        return $questions;
    }

    /**
     * 获取材料
     *
     * @param array $materialIds 材料id
     * @return array
     */
    private function getInterviewMaterial($materialIds) {
        global $cmsdb;

        $string = implode(',', $materialIds);
        $sql = "SELECT * FROM `interview_question_material` WHERE `id` IN ({$string})";
        $materials = (array)$cmsdb->getResults($sql);

        $list = [];
        foreach ($materials as $m) {
            $list[$m->id] = $m->material;
        }

        return $list;
    }
}